﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using qna.Business;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Script.Services;

namespace qna.Services {
	/// <summary>
	/// Summary description for Question
	/// </summary>
	[WebService(Namespace = "http://localhost/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	[ScriptService]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	// [System.Web.Script.Services.ScriptService]
	public class QuestionService : System.Web.Services.WebService {

		[WebMethod]
		public Question Get(int id) {
			return (Question)(new Business.Question()).GetById(id);
		}

		[WebMethod]
		public Question[] List(int[] subjects, string from, int rating, int limit) {
			DateTime date;
			if (from == "") {
				date = DateTime.Now;
			} else {
				DateTime.TryParse(from, out date);
				if (date == DateTime.MinValue)
					date = DateTime.Now;
			}
			return (new Question()).GetBySubject(subjects, date, rating, limit).ToArray();
		}

		public Question[] List(int[] subjects, string from = "", int rating = 0) {
			return List(subjects, from == "" ? DateTime.Now.ToString() : from, rating, 5);
		}

		public Question[] List(int[] subjects, int rating) {
			return List(subjects, DateTime.Now.ToString(), rating);
		}

		[WebMethod]
		public Question Create(string title, string content, int subject, int user) {
			Question q = new Question();
			q.Title = title;
			q.Content = content;
			q.SubjectId = subject;
			q.UserId = user;
			var attrs = q.Attributes;
			attrs.Remove("Answer_Id");
			return q.Insert(attrs) == 0 ? null : q;
		}

		[WebMethod]
		public Question Update(int id, string title, string content, int subject) {
			Question q = Get(id);
			if (q == null) {
				return null;
			} else {
				q.Title = title;
				q.Content = content;
				q.SubjectId = subject;
				var attrs = q.Attributes;
				attrs.Remove("Answer_Id");
				q.Update(attrs);
				return q;
			}
		}

		[WebMethod]
		public bool Delete(int id) {
			Question q = Get(id);
			if (q != null) {
				q.Id = id;
				return q.Delete();
			}
			return false;
		}

		[WebMethod]
		public bool SelectAnswer(int aID, int qID, int uID) {
			Question q = Get(qID);
			if (q != null) {
				q.AnswerId = aID;
				return q.SelectAnswer(aID, qID, uID);
			}
			return false;
		}

		[WebMethod]
		public bool SetQuestionRating(int qID, int uID) {
			Question q = Get(qID);
			bool returnVal = q.SetQuestionRating(qID, uID);

			return returnVal;
		}
		[WebMethod]
		public int GetQuestionRating(int qID) {
			Question q = Get(qID);
			int returnVal = q.GetQuestionRating(qID);

			return returnVal;
		}

		[WebMethod]
		public bool CheckIfUserRated(int qID, int uID) {
			Question q = Get(qID);
			bool returnVal = q.CheckIfUserRated(qID, uID);

			return returnVal;
		}
	}
}
